/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sle_announce_callback_stub.h"
#include "sle_hilog.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
SleAnnounceCallbackStub::SleAnnounceCallbackStub()
{
    memberFuncMap_[SleAnnounceCallbackInterfaceCode::SLE_CALLBACK_ANNOUNCE_STATE_CHANGE] =
        &SleAnnounceCallbackStub::OnAnnounceStateChangeInner;
}

SleAnnounceCallbackStub::~SleAnnounceCallbackStub(){};

int SleAnnounceCallbackStub::OnRemoteRequest(
    uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
{
    SLE_LOGD("cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
    std::u16string descriptor = SleAnnounceCallbackStub::GetDescriptor();
    std::u16string remoteDescriptor = data.ReadInterfaceToken();
    SLE_ASSERT_AND_RETURN_RET_LOG(
        descriptor == remoteDescriptor, ERR_INVALID_STATE, "local descriptor is not equal to remote error!");

    auto itFunc = memberFuncMap_.find((SleAnnounceCallbackInterfaceCode)code);
    if (itFunc != memberFuncMap_.end()) {
        auto memberFunc = itFunc->second;
        if (memberFunc != nullptr) {
            return (this->*memberFunc)(data, reply);
        }
    }
    SLE_LOGW("default case, need check.");
    return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
}

int SleAnnounceCallbackStub::OnAnnounceStateChangeInner(MessageParcel &data, MessageParcel &reply)
{
    SLE_LOGD("OnAnnounceStateChangeInner In");
    uint8_t id;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadUint8(id), TRANSACTION_ERR, "read announce id failed!");

    int32_t state;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadInt32(state), TRANSACTION_ERR, "read announce state failed!");

    bool terminal;
    SLE_ASSERT_AND_RETURN_RET_LOG(data.ReadBool(terminal), TRANSACTION_ERR, "read announce is or not teminal failed!");

    OnAnnounceStateChange(id, state, terminal);
    return NO_ERROR;
}
} // SleStandard
} // NearLink
} // OHOS